package com.lolongmaps.main;

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.util.Log;
import com.lolongmaps.model.ParsedData;


public class WeatherConnector {

	private String MY_DEBUG_TAG = "WeatherForcaster";
	public ParsedData parsedData;
	public WeatherMessageHandler weatherMessageHandler;

	WeatherConnector() {

		weatherMessageHandler = new WeatherMessageHandler();

		try {

			//URL url = new URL("https://www.google.com/ig/api?weather=Makati&hl=en&referrer=googlecalendar");
			URL url = new URL("http://173.194.35.176/ig/api?weather=Makati");

			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			SAXParser saxParser = saxParserFactory.newSAXParser();
			XMLReader xmlReader = saxParser.getXMLReader();
			WeatherHandler weatherHandler = new WeatherHandler();

			xmlReader.setContentHandler(weatherHandler);
			xmlReader.parse(new InputSource(url.openStream()));

			this.parsedData = weatherHandler.getParsedData();

		} catch (Exception e) {

			System.out.println("eto error" + e.getMessage());
			Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);

		}
	}

	public String returnSuggestion() {

		weatherMessageHandler.generateClothingSuggestion(parsedData
				.getParsedForecast());

		return weatherMessageHandler.getClothingSuggestion();
	}
}